package com.kaijie.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LPKun
 * @date 2021/4/13 15:12:55
 * 项目共享方法类
 */
@Slf4j
public class BaseUtils {


    /**
     * 将字符串匿名状态返回
     *
     * @param number 银行卡号
     * @return 后面4位字符除外其他全部替换成“*”符
     */
    public static String replaceNumber(String number) {
        if (StringUtils.isBlank(number)) return "";
        number = number.replace(" ", "");
        if (number.length() < 4) return "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < number.length() - 4; i++) {
            sb.append("*");
        }
        return sb.append(number.substring(number.length() - 4)).toString();
    }


    /**
     * 生成唯一号码，如订单号、支付单号等
     *
     * @return
     */
    public static Long genUniqueNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        Random rdm = new Random();
        String raw = sdf.format(new Date()) + rdm.ints(100000, 999999).findFirst().getAsInt();
        return Long.valueOf(raw);
    }

    /**
     * rest controller 返回数据转换
     *
     * @param list
     * @param values 需要返回的字段
     * @return
     */
    public static List<Map<String, Object>> restData(List<?> list, String... values) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map;
        for (Object object : list) {
            map = restData(object, values);
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * rest controller 返回数据转换
     *
     * @param object
     * @param values 需要返回的字段
     * @return
     */
    public static Map<String, Object> restData(Object object, String... values) {
        Class<? extends Object> clazz;
        Method method;
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < values.length; i++) {
            try {
                clazz = object.getClass();
                method = clazz.getMethod("get" + upperFirstCase(values[i]));
                map.put(values[i], method.invoke(object));
            } catch (Exception e) {
               // log.error(e.getMessage(), e);
            }
        }
        return map;
    }

    /**
     * 转换首字母大写
     *
     * @param str
     * @return
     */
    private static String upperFirstCase(String str) {
        char[] chars = str.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char) (chars[0] - 32);
            return new String(chars);
        }
        return str;
    }

    /**
     * 获取加密盐值
     *
     * @return
     */
    public static String generateSalt() {
        int length = 5;
        String str = "._-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static boolean regexMatch(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 删除属性=null
     */
    public static String removeNull(Object o) {
        Map<String, Object> map = JsonUtils.toMap(JsonUtils.toJsonString(o), String.class, Object.class);
        if (map == null)
            return "{}";
        map.keySet().removeIf(s -> map.get(s) == null);
        return JsonUtils.toJsonString(map);
    }
}
